package eg.edu.eelu.fyp2013.jdetector.core.swt;

import java.util.ArrayList;

import com.mathworks.toolbox.javabuilder.MWException;
import com.mathworks.toolbox.javabuilder.MWNumericArray;

import PCA.PCA;
import matlabcontrol.MatlabConnectionException;
import matlabcontrol.MatlabInvocationException;
import matlabcontrol.MatlabProxy;
import matlabcontrol.MatlabProxyFactory;
import matlabcontrol.extensions.MatlabNumericArray;
import matlabcontrol.extensions.MatlabTypeConverter;
import eg.edu.eelu.fyp2013.jdetector.core.input_output.ClassiferData;
import eg.edu.eelu.fyp2013.jdetector.core.input_output.FeatureExtraction;
import eg.edu.eelu.fyp2013.jdetector.core.input_output.ReducedData;

public class ReductionDeminsion_PCA {

	
	public ClassiferData [] reducefeatures (ArrayList<ArrayList<FeatureExtraction>> FeaturedMatixList
			 , int levels ) throws  MWException{
	{
	
		

		 ReducedData [][] Alll = null;
		 
		 int sizeimages = 0;
		 int sizelevels = 0 ;
		 
		 
		 
		for(int i = 0 ; i < FeaturedMatixList.size() ; i++) // num of levels
		{
		
			if( i == 0)
			{
			 Alll = new ReducedData [FeaturedMatixList.get(i).size()][FeaturedMatixList.size()];
			 sizeimages = FeaturedMatixList.get(i).size();
			 sizelevels = FeaturedMatixList.size();
			}
			
				for(int j = 0; j < FeaturedMatixList.get(i).size(); j++) // num of images 
				   {
			        
				    //processor.setNumericArray("Horz", new MatlabNumericArray(FeaturedMatixList.get(i).get(j).extractImagechd,null));
				    //processor.setNumericArray("vert", new MatlabNumericArray(FeaturedMatixList.get(i).get(j).extractImagecvd,null));
				    //processor.setNumericArray("diag", new MatlabNumericArray(FeaturedMatixList.get(i).get(j).extractImagecdd,null));
				    //processor.setNumericArray("All", new MatlabNumericArray(FeaturedMatixList.get(i).get(j).extractImageAll,null));
				    //proxy.eval("[H , V , D ,All] = PCAReduce(Horz, vert, diag, All)");
					PCA P = new PCA ();
					 
				    Object [] PC = new Object [4];
				    PC[0] = FeaturedMatixList.get(i).get(j).extractImagechd;
				    PC[1] = FeaturedMatixList.get(i).get(j).extractImagecvd;
				    PC[2] = FeaturedMatixList.get(i).get(j).extractImagecdd;
				    PC[3] = FeaturedMatixList.get(i).get(j).extractImageAll;
				    
				    MWNumericArray [] M = new MWNumericArray[4];
				    
				    
				    P.PCAReduce(M,PC);
				    
				    
				    double H = M[0].getDouble();
				    double V = M[1].getDouble();
				    double D = M[2].getDouble();
				    double all = M[3].getDouble();
				    
				    ReducedData R = new ReducedData();
				    
				    R.horz =  H;
				    R.vert = V;
				    R.diag = D;
				    R.all = all;
				    
				    //R.horz = processor.getNumericArray("H").getRealValue(0);
				    //R.vert= processor.getNumericArray("V").getRealValue(0);
				    //R.diag =  processor.getNumericArray("D").getRealValue(0);
				    //R.all =  processor.getNumericArray("All").getRealValue(0);
				    R.label = FeaturedMatixList.get(i).get(j).label;
				    R.level = i;
	                
				    Alll[j][i] = R;
			  
			}
			
			
			
		}
		
		
		ClassiferData [] FinalFeature = new ClassiferData[sizeimages];
        for(int m = 0; m < sizeimages; m++)
	    {
        	ClassiferData CD = new ClassiferData();
        	CD.Allreduceddata = new ArrayList<Double>();
        	
			for(int n = 0; n < sizelevels; n++)
			{
				
				
				CD.Allreduceddata.add(Alll[m][n].horz);
				CD.Allreduceddata.add(Alll[m][n].vert);
				CD.Allreduceddata.add(Alll[m][n].diag);
				CD.Allreduceddata.add(Alll[m][n].all);
				
				
			
			}
			CD.label = Alll[m][sizelevels - 1].label;
			FinalFeature[m] = CD;
			
	    }
		
        System.out.print("PCA one completed");
        return FinalFeature;
		
		
		
	}
	}	
}	   
	
